package org.jtb.quickr;

import java.util.Properties;
import java.util.Set;

import org.jtb.quickr.PhotosSearchCriteria.Extra;

/**
 * Quickr People service.
 */
public class PeopleService extends Service {
    /**
     * Construct a new People service and get quickr.properties from the class path.
     * @throws QuickrException If there was a problem constructing this object.
     */
    public PeopleService() throws QuickrException {
        super();
    }    
    
    /**
     * Construct a new quickr people service based on the given properties.
     * @param properties The quickr properties.
     * @throws QuickrException If a problem occurs constructing this object.
     */
    public PeopleService(Properties properties) throws QuickrException {
        super(properties);
    }
    
    /**
     * Get information about a user.
     * For details, see the 
     * <a href="http://www.flickr.com/services/api/flickr.people.getInfo.html">
     * flickr.people.getInfo service</a> documentation.
     * @param nsid The user ID.
     * @return The person.
     * @throws QuickrException If there was a problem getting the user's info.
     */
    public PersonResponse getInfo(String nsid) throws QuickrException {
        PeopleGetInfoRequest gpireq = new PeopleGetInfoRequest(properties.getRestUrl(), properties.getApiKey(), nsid, properties.getStaticUrl());
        PersonResponse pres = gpireq.execute();

        return pres;            
    }
    
    /**
     * Get public photos for a user.
     * For details, see the 
     * <a href="http://www.flickr.com/services/api/flickr.people.getPublicPhotos.html">
     * flickr.people.getPublicPhotos service</a> documentation.
     * @param nsid The user ID.
     * @throws QuickrException If a problem occurs getting the user's photos.
     * @return The photos.
     */
    public PhotosResponse getPublicPhotos(String nsid) throws QuickrException {
        return getPublicPhotos(nsid, null);
    }    
    
    /**
     * Get public photos for a user.
     * For details, see the 
     * <a href="http://www.flickr.com/services/api/flickr.people.getPublicPhotos.html">
     * flickr.people.getPublicPhotos service</a> documentation.
     * @param nsid The user ID.
     * @param extras Extra information to fetch for each returned record. Supported fields by Flickr are: 
     *      license, date_upload, date_taken, owner_name, icon_server, original_format, 
     *      last_update, geo, tags, machine_tags, o_dims, views, media.
     * @throws QuickrException If a problem occurs getting the user's photos.
     * @return The photos.
     */
    public PhotosResponse getPublicPhotos(String nsid, Set<Extra> extras) throws QuickrException {
        PeopleGetPublicPhotosRequest gppreq = new PeopleGetPublicPhotosRequest(properties.getRestUrl(), properties.getApiKey(), nsid, properties.getStaticUrl());
        
        gppreq.setExtras(extras);
        
        PhotosResponse pres = gppreq.execute();

        return pres;            
    }    

    /**
     * Get public groups for a user.
     * For details, see the 
     * <a href="http://www.flickr.com/services/api/flickr.people.getPublicGroups.html">
     * flickr.people.getPublicGroups service</a> documentation.
     * @param nsid The user ID.
     * @throws QuickrException If a problem occurs getting the user's photos.
     * @return The groups.
     */
    public GroupsResponse getPublicGroups(String nsid) throws QuickrException {
        PeopleGetPublicGroupsRequest gpgr = new PeopleGetPublicGroupsRequest(properties.getRestUrl(), properties.getApiKey(), nsid);
        GroupsResponse gres = gpgr.execute();

        return gres;            
    }
    
    /**
     * Find a user ID by user name.
     * For details, see the 
     * <a href="http://www.flickr.com/services/api/flickr.people.findByUsername.html">
     * flickr.people.findByUsername service</a> documentation.
     * @param userName The user name.
     * @throws QuickrException If a problem occurs.
     * @return The user, including it's user ID.
     */
    public UserResponse findByUserName(String userName) throws QuickrException {
        PeopleFindByUsernameRequest fbureq = new PeopleFindByUsernameRequest(properties.getRestUrl(), properties.getApiKey(), userName);
        UserResponse ures = fbureq.execute();

        return ures;                    
    }

    /**
     * Find a user ID by email address.
     * For details, see the 
     * <a href="http://www.flickr.com/services/api/flickr.people.findByEmail.html">
     * flickr.people.findByEmail service</a> documentation.
     * @param email The email address.
     * @throws QuickrException If a problem occurs.
     * @return The user, including it's user ID.
     */
    public UserResponse findByEmail(String email) throws QuickrException {
        PeopleFindByEmailRequest fber = new PeopleFindByEmailRequest(properties.getRestUrl(), properties.getApiKey(), email);
        UserResponse ures = fber.execute();

        return ures;                    
    }
    
    /**
     * Get a user's upload status.
     * For details, see the 
     * <a href="http://www.flickr.com/services/api/flickr.people.getUploadStatus.html">
     * flickr.people.getUploadStatus service</a> documentation.
     * @param nsid The user ID.
     * @throws QuickrException If a problem occurs getting the user's photos.
     * @return The upload status.
     */
    public UserResponse getUploadStatus(AuthResponse.Token authToken, String nsid) throws QuickrException {
        PeopleGetUploadStatusRequest gusr = new PeopleGetUploadStatusRequest(properties.getRestUrl(), properties.getApiKey(), properties.getSecret(), authToken, nsid);
        UserResponse ur = gusr.execute();

        return ur;            
    }    
}
